package page.actionPage;

import com.alibaba.fastjson2.JSONObject;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Image;
import org.sikuli.script.Screen;
import utils.Constant;

public class MousePageController extends ActionPage {
    private final String ACTION = "action";
    @FXML
    private FindImgBasePageController findImgBasePageController;
    @FXML
    private ChoiceBox<String> action;

    public MousePageController() {
        super("mouse", "鼠标操作", Constant.TYPE.ACTION);
    }

    @Override
    public void run(JSONObject data) {
        Image image = FindImgBasePageController.getImg(data);
        Screen s = new Screen();
        s.setAutoWaitTimeout(10);
        try {
            if (data.getString(ACTION).equals("左键点击")) {
                s.click(image);
            } else if (data.getString(ACTION).equals("双击")) {
                s.doubleClick(image);
            } else if (data.getString(ACTION).equals("右键点击")) {
                s.rightClick(image);
            } else if (data.getString(ACTION).equals("移动")) {
                s.mouseMove(image);
            }
        } catch (FindFailed e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void initAction() {
        findImgBasePageController.initAction(this);
        templateController.initTemplate(submitButton -> {
                    findImgBasePageController.saveImageInDB(db);
                    db.saveInData(Constant.DB.MAIN_PAGE_BUTTON_NAME, CHINESE_NAME);
                    db.saveInData(ACTION, action.getValue());
                    return this;
                }
        );
        action.getItems().add("左键点击");
        action.getItems().add("双击");
        action.getItems().add("右键点击");
        action.getItems().add("移动");
        action.setValue((String) db.getDataOrDefault(ACTION, "左键点击"));
    }
}
